

from pykafka import KafkaClient


client = KafkaClient(
    hosts="192.168.101.10:9093,192.168.101.10:9094,192.168.101.10:9095")  # 可接受多个Client这是重点
# client.topics  # 查看所有topic
topic = client.topics[b'my.test']  # 选择一个topic

consumer = topic.get_balanced_consumer(
    consumer_group=b'testgroup',
    # consumer_id='testid',
    auto_commit_enable=True,  # 设置为Flase的时候不需要添加 consumer_group
    zookeeper_connect='192.168.101.10:2182,192.168.101.10:2183,192.168.101.10:2184'  # 这里就是连接多个zk
)


for message in consumer:
    if message is not None:
        #打印接收到的消息体的偏移个数和值
        print(message.offset, message.value)
    else:
        print('message is None')
